<?php
/*
 * @Descripttion: 
 * @Author: dedeye
 * @Date: 2020-02-27 17:00:34
 * @LastEditors: dedeye
 * @LastEditTime: 2020-02-28 23:00:47
 */

namespace app\admin\controller;

use app\admin\model\RuleGroup as RuleGroupModel;
use app\common\util\ReturnCode;

class RuleGroup extends Controller
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        if(!request()->isAjax()) {
            return $this->fetch();
        }
        $field = '';
        $group = RuleGroupModel::field($field)->append(['status_text'])->all();
        if($group->isEmpty()) {
            return resultJson(ReturnCode::RESULT_NULL, '暂无数据');
        }
        return resultJson(ReturnCode::SUCCESS, 'success', $group);
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        if(!request()->isPost() || !request()->isAjax())
            return $this->fetch('add');
        $data = input('post.');
        // $checkData = self::checkData(new AdminValidate, 'add', $data);
        // if(!is_null($checkData)) {
        //     return resultJson(ReturnCode::PARAM_ERROR, $checkData);
        // }
        $group = new RuleGroupModel;
        $addRes = $group->allowField(true)->save($data);
        if(!$addRes) {
            return resultJson(ReturnCode::RESULT_FAIL, '添加失败');
        }
        return resultJson(ReturnCode::SUCCESS, '添加成功');
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        $group = RuleGroupModel::get($id);
        if(is_null($group)) {
            return resultJson(ReturnCode::RESULT_NULL, '规则组不存在');
        }
        return resultJson(ReturnCode::SUCCESS, 'success', $group);
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit()
    {
        if(!request()->isPost() || !request()->isAjax())
            return $this->fetch('edit');
        $data = input('post.');
        // $checkData = self::checkData(new AdminValidate, 'add', $data);
        // if(!is_null($checkData)) {
        //     return resultJson(ReturnCode::PARAM_ERROR, $checkData);
        // }
        $id = $data['id'];
        unset($data['id']);
        $group = new RuleGroupModel;
        $updateRes = $group->allowField(true)->save($data, ['id' => $id]);
        if(!$updateRes) {
            return resultJson(ReturnCode::RESULT_FAIL, '修改失败');
        }
        return resultJson(ReturnCode::SUCCESS, '修改成功');
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        $group = RuleGroupModel::get($id);
        if(is_null($group)) {
            return resultJson(ReturnCode::RESULT_NULL, '规则不存在');
        }
        $delRes = $group->delete();
        if(!$delRes) {
            return resultJson(ReturnCode::RESULT_FAIL, '删除失败');
        }
        return resultJson(ReturnCode::SUCCESS, '删除成功');
    }

    /**
     * 用户组授权
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function auth()
    {
        return $this->fetch('auth');
    }
    
    public function authtree($id)
    {
        $rule = new \app\admin\model\Rule;
        $list = $rule->field('id, name, fid')->where('status', 1)->all();
        if($list->isEmpty()) {
            return resultJson(ReturnCode::RESULT_NULL, '规则不存在');
        }
        $groupRules = RuleGroupModel::field('rules')->get($id);
        if(is_null($groupRules)) {
            return resultJson(ReturnCode::RESULT_NULL, '用户组不存在');
        }
        $checkedId = [];
        if('' != $groupRules->rules) {
            $checkedId = explode(",", $groupRules->rules);
            foreach($checkedId as $k => $v) {
                $checkedId[$k] = (int) $v;
            }
        }
        return resultJson(ReturnCode::SUCCESS, 'success', compact('list', 'checkedId'));
    }

    public function saveauth()
    {
        $data = input('post.');
        if(!isset($data['authids']) || !is_array($data['authids'])) {
            return resultJson(ReturnCode::PARAM_ERROR, '未选中规则节点');
        }
        $authids = '';
        foreach($data['authids'] as $k => $v) {
            if(!is_numeric($v)) {
                return resultJson(ReturnCode::PARAM_ERROR, '选中节点id有误');
            }
            $authids .= $v . ',';
        }
        $authids = rtrim($authids, ',');
        $group = RuleGroupModel::get($data['id']);
        if(is_null($group)) {
            return resultJson(ReturnCode::PARAM_ERROR, '授权用户id错误');
        }
        $group->rules = $authids;
        $authRes = $group->save();
        if(!$authRes) {
            return resultJson(ReturnCode::RESULT_ERROR, '授权失败');
        }
        return resultJson(ReturnCode::SUCCESS, '授权成功', $authids);
    }
}
